package pathfinding;

import java.util.ArrayList;
import java.util.Random;

import base.Ship;
import base.Timestamped;

public class ObstaclePathfinder implements IPathfinder<GridCell> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Random r;
	GridCell origo;
	int rangeLimit;
	
	public ObstaclePathfinder(int rangeLimit, Ship s)
	{
		r = new Random();
		this.rangeLimit = rangeLimit;
		origo = GridGeometry.getCell(s);
	}
	
	@Override
	public ArrayList<Timestamped<GridCell>> getPath(GridCell start,
			GridCell goal) {
		ArrayList<Timestamped<GridCell>> plan = new ArrayList<Timestamped<GridCell>>(2);
		plan.add(new Timestamped<GridCell> (start, 0));
		
		// don't care about given goal, instead pick a random square within rangelimit
		int x = origo.x + (r.nextInt() % (rangeLimit * 2)) - rangeLimit;
		int y = origo.y + (r.nextInt() % (rangeLimit * 2)) - rangeLimit;
		plan.add(new Timestamped<GridCell> (new GridCell(x, y), Long.MAX_VALUE));
		return plan;
	}

	@Override
	public double getCost(ArrayList<Timestamped<GridCell>> path) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean needsUpdate() {
		double chancePerSecond = 1/20.0;
		double chance = DriveWithPathfinding.PERIOD_MS / 1000.0 * chancePerSecond;
		
		return (r.nextDouble() < chance);
	}

}
